package com.pfzy.wechatpay

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.view.View
import android.widget.Button
import com.tencent.mm.opensdk.modelbiz.WXLaunchMiniProgram

import android.content.Context
import android.widget.Toast
import com.pfzy.wechatpay.dialog.ShareDialog
import com.pfzy.wechatpay.third.ali.IPay
import com.pfzy.wechatpay.third.ali.PayHelper
import com.pfzy.wechatpay.third.ali.PayListener
import com.pfzy.wechatpay.third.wechat.Constants
import com.pfzy.wechatpay.third.wechat.WeiXinPay
import com.pfzy.wechatpay.third.wechat.WxUtils
import com.tencent.mm.opensdk.constants.ConstantsAPI
import com.tencent.mm.opensdk.modelbase.BaseReq
import com.tencent.mm.opensdk.modelbase.BaseResp
import com.tencent.mm.opensdk.modelpay.PayReq
import com.tencent.mm.opensdk.openapi.IWXAPIEventHandler
import com.tencent.mm.opensdk.openapi.WXAPIFactory
import java.lang.ref.WeakReference

class MainActivity : AppCompatActivity(), IWXAPIEventHandler {
    private var mContext: Context? = null
    private var regBtn: Button? = null
    private var ShareBtn: Button? = null

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        mContext = this

        regBtn = findViewById<View>(R.id.wechat_pay) as Button
        regBtn!!.setOnClickListener(View.OnClickListener {
            wechatPay()
        })

        ShareBtn = findViewById<View>(R.id.wechat_share) as Button
        ShareBtn!!.setOnClickListener(View.OnClickListener {
            ShareDialogShow()
        })
    }

    /**
     * 显示分享底部区域
     */
    private fun ShareDialogShow(){
        val bundle = Bundle()
        val shareDialog: ShareDialog = ShareDialog.getInstance(mContext, bundle)
        shareDialog.setOnShareClickLitener(object : ShareDialog.OnShareClickLitener {
            override fun onShareToQQ() {
                openShare("QQ")
            }

            override fun onShareToQZone() {
                openShare("QZone")
            }

            override fun onShareToWX() {
                mContext?.let {
                    WxUtils.share(
                        it,
                        "司机邀请",
                        "加入麦田，让出行更温暖",
                        "www.baidu.com",0,
                        "https://data.maitianshanglv.com/zyxq/20220215/FkGcbILyNr3SzKyeRSVaTo2jfh3o.png"
                    )
                };
            }

            override fun onShareToWXCircle() {
                openShare("WXCircle")
            }

            override fun onShareToSina() {
                openShare("Sina")
            }
        })
        shareDialog.show(supportFragmentManager, "Share")
    }

    // 在这里可以配合友盟分享，通过switch语句，根据type判断平台。执行分享代码
    private fun openShare(type: String) {
        Toast.makeText(mContext, type, Toast.LENGTH_SHORT).show()
    }

    /**
     * 微信支付
     */
    fun wechatPay() {
        val request = PayReq()
        request.appId = Constants.APP_ID
        request.partnerId = "1900000109"
        request.prepayId = "1101000000140415649af9fc314aa427"
        request.packageValue = "Sign=WXPay"
        request.nonceStr = "1101000000140429eb40476f8896f4c9"
        request.timeStamp = "1398746574"
        request.sign = "7FFECB600D7157C5AA49810D2D8F28BC2811827B"
//            PayHelper(WeiXinPay(this, request)).startPay(payListener)
    }
    /**
     * 微信小程序支付
     */
    fun wechatminiPay(){
        val appId = Constants.APP_ID // 填移动应用(App)的 AppId，非小程序的 AppID

        val req = WXLaunchMiniProgram.Req()
        req.userName = "gh_******" // 填小程序原始id
        req.path = "pages/appPay/index" ////拉起小程序页面的可带参路径，不填默认拉起小程序首页，对于小游戏，可以只传入 query 部分，来实现传参效果，如：传入 "?foo=bar"。
        req.miniprogramType = WXLaunchMiniProgram.Req.MINIPROGRAM_TYPE_PREVIEW // 可选打开 开发版，体验版和正式版

        val api = WXAPIFactory.createWXAPI(this@MainActivity, appId)
        api.sendReq(req)
    }

    fun AliPay(){
//        PayHelper(AliPay("", this)).startPay(payListener)
    }

    private val payListener = object : PayListener {
        override fun success(payHelper: IPay?) {
            super.success(payHelper)
        }
    }

    override fun onReq(p0: BaseReq?) {
        Toast.makeText(this, "onReq", Toast.LENGTH_LONG).show()
    }

    override fun onResp(p0: BaseResp?) {
        if (p0 != null) {
            if ( p0.getType() === ConstantsAPI.COMMAND_LAUNCH_WX_MINIPROGRAM) {
                val launchMiniProResp = p0 as WXLaunchMiniProgram.Resp
                val extraData = launchMiniProResp.extMsg //对应小程序组件 <button open-type="launchApp"> 中的 app-parameter 属性
                Toast.makeText(this, extraData, Toast.LENGTH_LONG).show()
            }
        }
    }
}
